package leetcode_300;

import java.util.HashMap;
import java.util.Map;

/**
 *@author 周杨
 *WordPattern_290 字典匹配 
 *describe:双哈希 AC 96%
 *2018年6月29日 上午10:03:11
 */
public class WordPattern_290 {

	public static void main(String[] args) {
		WordPattern_290 test=new WordPattern_290();
		//System.out.println(test.wordPattern("abba", "dog cat cat dog"));
		System.out.println(test.wordPattern("ab", "dog dog"));

	}
	public boolean wordPattern(String pattern, String str) {
        String[] arrays=str.split(" ");
        if(pattern.length()!=arrays.length)
        	return false;
        Map<Character,String> hash1=new HashMap<Character,String>();
        Map<String,Character> hash2=new HashMap<String,Character>();
        for(int i=0;i<pattern.length();++i) {
        	char now=pattern.charAt(i);
        	String mapString=hash1.get(now);
        	if(mapString==null) {
        		hash1.put(now, arrays[i]);
        	}
        	else if(!mapString.equals(arrays[i]))
        		return false;
        	
        	Character mapChar=hash2.get(arrays[i]);
        	if(mapChar==null) {
        		hash2.put(arrays[i], now);
        		continue;
        	}
        	if(mapChar!=now)
        		return false;
        }
        return true;
    }
}
